﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;
using System.Text;

public partial class act_in : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private MemberFactory m_MemberFactory;
    private IPostService m_PostService;
    private IMemberService m_MemberService;
    private ISignUpService m_SignUpService;
    private string m_FinishUrl = "act-all.aspx";
    private WebMailService m_WebMailService = new WebMailService();

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();
        m_SignUpService = m_PostFactory.GetSignUpService();

        if (!IsPostBack)
        {
            LodaDataToUI();
        }
    }

    private void LodaDataToUI()
    {
        int id = QueryStringHelper.GetInteger("id", 0);
        PostVO postVO = m_PostService.GetPostById(id);

        if (postVO != null)
        {
            NodeVO nodeVO = m_PostService.GetNodeById(postVO.Node.NodeId);
            ltlTitle.Text = string.Format("{0}-{1}", nodeVO.Name, postVO.Title);
            ltlHtmlContent.Text = postVO.HtmlContent;
        }
    }

    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        if (!rdoIsMemberY.Checked && !rdoIsMemberN.Checked)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請選擇是否為會員。"), false);
            return;
        }
        if (!ckbAgree.Checked)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("您尚未勾選同意會員服務條款。"), false);
            return;
        }

        SignUpVO signUpVO = new SignUpVO();
        UIHelper.FillVO(UpdatePanel1, signUpVO);
        //生日
        signUpVO.BirthDay = ucDate.Date;
        //會員
        if (rdoIsMemberY.Checked)
        {
            MemberVO memberVO = m_MemberService.GetMemberByLoginId(txtLoginId.Text.Trim());
            if (memberVO != null && memberVO.LoginPassword.Equals(txtLoginPassword.Text.Trim()))
            {
                signUpVO.Member = memberVO;
                signUpVO.Name = memberVO.Name;
                signUpVO.Email = memberVO.Email;
                signUpVO.Mobile = memberVO.Mobile;
                signUpVO.BirthDay = memberVO.BirthDay;
                signUpVO.Company = memberVO.Company;
                signUpVO.JobTitle = memberVO.JobTitle;

            }
        }

        if (rdoRecommendY.Checked)
        {
            signUpVO.RecommendName = txtRecommendName.Text.Trim();
        }

        //日期與時間
        signUpVO.CreateTime = DateTime.Now;
        signUpVO.UpdateTime = DateTime.Now;
        //對應的活動
        int postId = QueryStringHelper.GetInteger("id", 0);
        PostVO postVO = m_PostService.GetPostById(postId);
        signUpVO.Post = postVO;

        if (m_SignUpService.GetSignUpByPostIdAndEmail(postId, signUpVO.Email))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("您已經報名過此活動了。"), false);
            return;
        }

        //判斷必填與錯誤
        if (CheckErrorOK(signUpVO))
        {
            m_SignUpService.CreateSignUp(signUpVO);
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("感謝您的支持，已送出報名。", m_FinishUrl), false);
        }
    }

    private bool CheckErrorOK(SignUpVO signUpVO)
    {
        bool result = true;
        StringBuilder sbError = new StringBuilder();

        if (rdoIsMemberY.Checked && signUpVO.Member == null)
        {
            sbError.Append("帳號或密碼錯誤。" + "\\n");
        }
        else if (rdoIsMemberY.Checked && signUpVO.Member != null && signUpVO.Member.Flag != 1)
        {
            sbError.Append("帳號已停用。" + "\\n");
        }
        else if (rdoIsMemberN.Checked)
        {
            //姓名必填
            if (string.IsNullOrEmpty(signUpVO.Name))
            {
                sbError.Append("請輸入您的中文姓名。" + "\\n");
            }
            //電子信箱
            if (string.IsNullOrEmpty(signUpVO.Email))
            {
                sbError.Append("請輸入您的電子信箱。" + "\\n");
            }
            //行動電話
            if (string.IsNullOrEmpty(signUpVO.Mobile))
            {
                sbError.Append("請輸入您的行動電話。" + "\\n");
            }

            //生日必填
            if (signUpVO.BirthDay == null)
            {
                sbError.Append("請選擇您的生日，或是您選擇的日期是錯誤的。" + "\\n");
            }
        }

        if (rdoRecommendY.Checked && string.IsNullOrEmpty(signUpVO.RecommendName))
        {
            sbError.Append("請輸入您的推薦人姓名。" + "\\n");
        }


        if (!string.IsNullOrEmpty(sbError.ToString()))
        {
            result = false;
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS(sbError.ToString()), false);
        }

        return result;
    }

    protected void lnkForgetPass_Click(object sender, EventArgs e)
    {
        string loginId = txtLoginId.Text.Trim();

        if (string.IsNullOrEmpty(loginId))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請輸入您的帳號。"), false);
            return;
        }

        MemberVO memberVO = m_MemberService.GetMemberByLoginId(loginId);
        if (memberVO == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("查無此帳號。"), false);
            return;
        }

        if (memberVO.Flag != 1)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("帳號已停用。"), false);
            return;
        }

        //發信
        m_WebMailService.SendMail_ToMember_ByForgetPassword(memberVO);

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("已寄出您的密碼，請至信箱收信。"), false);
    }
}